package com.cnswhy.cloud.server.core.appservice.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.cnswhy.cloud.common.core.exception.BizException;
import com.cnswhy.cloud.common.core.mybatis.conditions.Wraps;
import com.cnswhy.cloud.server.core.appservice.AppCustomerService;
import com.cnswhy.cloud.server.core.config.AlipayConfig;
import com.cnswhy.cloud.server.core.entity.Customer;
import com.cnswhy.cloud.server.core.entity.CustomerAlipay;
import com.cnswhy.cloud.server.core.entity.WithdrawalRecord;
import com.cnswhy.cloud.server.core.service.customer.CustomerAlipayService;
import com.cnswhy.cloud.server.core.service.customer.CustomerService;
import com.cnswhy.cloud.server.core.service.trans.WithdrawalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AppCustomerServiceImpl implements AppCustomerService {
    @Autowired
    CustomerService customerService;
    @Autowired
    WithdrawalRecordService withdrawalRecordService;
    @Autowired
    CustomerAlipayService customerAlipayService;
    public static final String WITHDRAW_VERIFY_SUCCESS = "withdraw_verify_success";

    public static final String WITHDRAW_VERIFY_REFUSE = "withdraw_verify_refuse";
    @Override
    public void resetCustomerSpellStatus() throws BizException {
        /*log.info("重置所有拼团用户的拼团状态");
        List<Customer> customerList = customerService.list(Wraps.<Customer>lbQ().eq(Customer::getIsSpell,2));
        for(Customer customer : customerList){
            customer.setIsSpell(1);
            customerService.updateById(customer);
        }*/
    }

    @Override
    public void processWithdraw(Long id, String status) throws BizException {
        /*WithdrawalRecord withdrawalRecord = withdrawalRecordService.getById(id);
        if(withdrawalRecord == null){
            return;
        }
        Customer customer = customerService.getById(withdrawalRecord.getCustomerId());
        if(customer == null){
            return;
        }
        if (WITHDRAW_VERIFY_SUCCESS.equals(status)) {
            if (withdrawalRecord.getType() == 2)
            {
                //支付宝
                log.info("支付宝提现：{}",withdrawalRecord.getBankId());
                CustomerAlipay alipayAccount = customerAlipayService.getById(withdrawalRecord.getBankId());
                CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
                certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
                certAlipayRequest.setAppId("2021001167646072");
                certAlipayRequest.setPrivateKey(AlipayConfig.RSA_PRIVATE_KEY_PAY);
                certAlipayRequest.setFormat("json");
                certAlipayRequest.setCharset("GBK");
                certAlipayRequest.setSignType("RSA2");
                certAlipayRequest.setCertPath("//home//aliCert//appCertPublicKey_2021001167646072.crt");
                certAlipayRequest.setAlipayPublicCertPath("//home//aliCert//alipayCertPublicKey_RSA2.crt");
                certAlipayRequest.setRootCertPath("//home//aliCert//alipayRootCert.crt");

                AlipayClient alipayClient = null;
                try {
                    alipayClient = new DefaultAlipayClient(certAlipayRequest);
                } catch (AlipayApiException e) {
                    e.printStackTrace();
                }

                AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
                request.setBizContent("{" + "\"out_biz_no\":\"" + withdrawalRecord.getOrderNum() + "\"," + "\"trans_amount\":"
                        + withdrawalRecord.getRealPay().toString() + "," + "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\","
                        + "\"biz_scene\":\"DIRECT_TRANSFER\"," +
                        // "\"original_order_id\":\"20190620110075000006640000063056\"," +
                        "\"payee_info\":{" + "\"identity\":\"" + alipayAccount.getAliAccount() + "\","
                        + "\"identity_type\":\"ALIPAY_LOGON_ID\"," + "\"name\":\"" + alipayAccount.getRealName() + "\""
                        + "    }," + "\"remark\":\"单笔转账\","
                        + "\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"" + "  }");
                try
                {
                    AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
                    log.error("================支付宝红包返回数据："+response.getBody());
                    if (response.isSuccess())
                    {
                        withdrawalRecord.setStatus(2);
                    }
                    else
                    {
                        withdrawalRecord.setStatus(3);
                    }

                }
                catch (Exception e)
                {
                    withdrawalRecord.setStatus(3);
                }
                withdrawalRecordService.updateById(withdrawalRecord);
                if (withdrawalRecord.getStatus() == 3)
                {
                    customer.setAvailableBalance(customer.getAvailableBalance().add(withdrawalRecord.getAmount()));
                    customerService.updateById(customer);
                }
            }
        } else if (WITHDRAW_VERIFY_REFUSE.equals(status)) {
            customer.setAvailableBalance(customer.getAvailableBalance().add(withdrawalRecord.getAmount()));
            customerService.updateById(customer);
        }else{
            return;
        }*/
        
    }
}
